﻿using UnityEditor;
using UnityEngine;  

namespace XFGameFramework.Localization
{
    [CustomEditor(typeof(LocalizeImage))]
    public class LocalizeImageInspector : Editor
    { 
        private SerializedProperty m_Script = null; 

        private SerializedProperty id = null;


        private LocalizeImage localizeImage = null;

        private GUIContent idContent = null;

        private int lastId;


        private void OnEnable()
        {
            localizeImage = target as LocalizeImage; 
            m_Script = serializedObject.FindProperty("m_Script");  
            id = serializedObject.FindProperty("id"); 
        }


        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUI.BeginDisabledGroup(true);

            EditorGUILayout.PropertyField(m_Script);

            EditorGUI.EndDisabledGroup();

            OnIDGUI(id);
             
            serializedObject.ApplyModifiedProperties();
        }


        private void OnIDGUI(SerializedProperty id)
        {
            LanguageInfo info = LocalizationManager.GetLanguageInfo(id.intValue);
            //if (info == null) return;

            EditorGUILayout.LabelField(string.Empty, "语言配置ID", "BoldLabel");
            GUILayout.BeginHorizontal();

            GUILayout.Label("ID");



            if (id.intValue != lastId)
            {
                idContent = null;
                lastId = id.intValue;
            }

            if (idContent == null) 
            { 
                idContent = new GUIContent();
                if(info != null)
                    idContent.image = LanguageConfigsInspector.GetImage(info.image.projectName, info.image.imageName);
            }

            if (info != null)
            {
                string projectName = string.IsNullOrEmpty(info.image.projectName) ? string.Empty : info.image.projectName;
                string imageName = string.IsNullOrEmpty(info.image.imageName) ? string.Empty : info.image.imageName;

                if(info.type == MultiLanguageType.Image) 
                    idContent.text = string.Format("{0}({1}/{2})",id.intValue,projectName, imageName);
                else if(info.type == MultiLanguageType.NetworkImage)
                    idContent.text = string.Format("{0}({1})", id.intValue, info.url);
            }
            else
            {
                idContent.text = string.Format("{0}(空)",id.intValue);
            }



            if (EditorGUILayout.DropdownButton(idContent, FocusType.Passive,GUILayout.MaxWidth(300)))
            {
                LanguageSearchWindow window = EditorWindow.GetWindow<LanguageSearchWindow>();
                window.Init(id, MultiLanguageType.Image);
            }

            GUILayout.EndHorizontal();
        }
    }

}

